package com.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pojo.view.PunchRecord;
import com.service.PunchRecordService;
import com.service.impl.PunchRecordServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/punchRecordServlet/*")
public class PunchRecordServlet extends BaseServlet{

    PunchRecordService punchRecordService = new PunchRecordServiceImpl();


    public void checkPunchRecord(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();
        JSONObject jsonObject = JSON.parseObject(params);

        String username = jsonObject.get("username").toString();

        List<PunchRecord> punchRecordList = punchRecordService.getAllPunchRecord(username);
        String toJSONString = JSON.toJSONString(punchRecordList);

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(toJSONString);
    }



}
